<template>
  <div class="search-result">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        >
  <van-cell v-for="item in searchResults" :key="item.art_id" :title="item.title"  @click="$router.push(`/essay/${item.art_id}`)"/>
</van-list>
  </div>
</template>

<script>
import { getSearchResult } from 'network/search'
export default {
  name: 'SearchResult',
  props: {
    searchContent: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      // 搜索结果参数
      searchParams: {
        page: 1,
        per_page: 10,
        q: this.searchContent
      },
      // 本次下拉加载是否完成
      loading: false,
      // 后台是否还能返回下一次数据
      finished: false,
      searchResults: []
    }
  },
  methods: {
    async onLoad () {
      const { data: res } = await getSearchResult(this.searchParams)
      this.searchResults.push(...res.data.results)
      this.loading = false
      console.log(this.searchResults)
      if (res.data.results.length < 10) {
        this.finished = true
      } else {
        this.searchParams.page++
      }
    }
  }
}
</script>

<style lang='less' scoped>
    .search-result{
        position: fixed;
        top: 54px;
        bottom: 0;
        left: 0;
        right: 0;
        overflow-y: auto;
    }
</style>
